package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @ResponseBody
    @PostMapping("list")
    public Map<String, Object> list(
            Integer page,
            Integer rows,
            String customerName
    ) {
        return customerService.listCustomer(page, rows, customerName);
    }

    @ResponseBody
    @PostMapping("save")
    public ServiceVO save(Integer customerId, Customer customer) {
        Customer customerForSave = new Customer();
        BeanUtils.copyProperties(customer, customerForSave);
        customer.setCustomerId(customerId);
        customerService.save(customerForSave);
        return new ServiceVO(100, "请求成功", null);
    }

    @ResponseBody
    @PostMapping("delete")
    public ServiceVO delete(String[] ids){
        customerService.deleteBatch(ids);
        return new ServiceVO(100, "请求成功", null);
    }
}
